package com.example.common.security.service.impl;

import com.example.common.security.component.CustomUser;
import com.example.common.security.service.ICustomUserDetailService;
import com.example.member.api.entity.Member;
import com.example.member.api.feign.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Service
public class CustomUserDetailServiceImpl implements ICustomUserDetailService {

    @Autowired
    MemberService memberService;

    @Override
    public UserDetails loadUserByMobile(String mobile) throws UsernameNotFoundException {
        return null;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Member member = memberService.getMmeberInfo(username);
        if (member == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        //设置权限
        Set<String> dbAuthsSet = new HashSet<>();
        Collection<? extends GrantedAuthority> authorities
                = AuthorityUtils.createAuthorityList(dbAuthsSet.toArray(new String[0]));
        return new CustomUser(member.getId().toString(),username,member.getPassword(),authorities);
    }
}
